package com.ququ.syb.withdraworder.service;

import com.ququ.syb.common.constant.IdSequenceConstant;
import com.ququ.syb.withdraworder.redis.RedisDao;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Configuration;
import org.springframework.stereotype.Service;
import redis.clients.jedis.Jedis;

import java.text.SimpleDateFormat;
import java.util.Date;

@Service
@Configuration
public class WithdrawOrderIdSequenceService {
    private static SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyyMMdd");

    @Autowired
    private RedisDao redisDao;

    @Value("${withdraw.tradenum.prefix}")
    private String prefix;

    public Long getNextId(){
        Jedis jedis = redisDao.getJedis();
        try {
            return jedis.incr(IdSequenceConstant.SEQ_WITHDRAWORDER_ID);
        }finally {
            redisDao.close(jedis);
        }
    }


    public String getNextTradenum(){
        Jedis jedis = redisDao.getJedis();
        try {
            String today = simpleDateFormat.format(new Date());
            Long orderseq = jedis.incr(IdSequenceConstant.SEQ_WITHDRAW_TRADENUM+today);
            return prefix+(Long.parseLong(today)*10000000l+orderseq);
        }finally {
            redisDao.close(jedis);
        }
    }

}
